/**
 * 页面加载完毕执行
 */
var contextPath;
$(function(){
	var parentFrame = window.parent;
	if(parentFrame && parentFrame != window){
		window.top.location.href = contextPath + "login.jsp";
	}
	$(".join_content").find("ul").empty();
	$(".page").empty();
	//加载单位
	$.ajax({
		url : contextPath+ 'user/company/list',
		data : {
			companyName : "",
			pageszie : 4,
			pagenum : 1
		},
		type : 'POST',
		success : function(list) {
			//alert(list.records.length);
			var companyname = "";
			var join = "";
			for(var i=0;i<list.records.length;i++){
				var company = list.records[i];
				//alert(company.refStatus);
				var refstatus = company.refStatus;
				var refname = '<p class="event" onclick="join(\''+companyname+'\',1,'+company.companyId+');">加入</p>';
				if(refstatus==1){
					refname = "<p>已加入</p>";
				}else if(refstatus==2){
					refname = "<p>待审核</p>";
				}
				join = join + "<li><a href='#'><span>"+company.companyId+" | "+company.companyName+"</span><em>"+company.address+"</em></a>"+refname+"</li>";
			}
			$(".join_content").find("ul").append(join);
			//分页
			var fullListSize = list.totalRecord;
		 	var pageNumber = list.currPage;
		 	var totalPage=0;
		 	if (fullListSize % 4 == 0) {
				totalPage = fullListSize / 4;
			} else {
				totalPage = Math.floor(fullListSize / 4 + 1);
			}
		 	//alert(fullListSize);
		 	//alert(pageNumber);
		 	//alert(totalPage);
		 	if(fullListSize!=undefined && fullListSize!=null && fullListSize>0){
		 		var start = 1;
	 			var end = totalPage;
	 			if(totalPage > 5 && pageNumber != 1) {
	 				if((totalPage-pageNumber)<3){
	 					start = pageNumber - (5 -(totalPage-pageNumber) -1);
	 				}else{
	 					start =  pageNumber -1;
	 				}
	 			} 
	 			if((totalPage-3)>pageNumber){
	 				if(pageNumber == 1) {
	 					end = 5;
	 				} else {
		 				end = pageNumber + 3;
	 				}
	 			}
	 			var prePage = pageNumber - 1 < 1 ? 1 : pageNumber - 1;
	 			var nextPage = pageNumber + 1 > totalPage ? totalPage : pageNumber + 1;
	 			
	 			$(".page").append('<a href="joincompany.jsp">首页</a> ');
	 			$(".page").append('<a href="javascript:goToPage(\''+companyname+'\','+prePage+')">上一页</a> ');
	 			for(var i=start;i<=end;i++){
		 			if(i == pageNumber){
		 				if(i < 5) {
		 					$str = $('<a class="intro" href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
		 				} else {
		 					$str = $('<a class="intro" href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
		 				}
			 		}else{
			 			if(i < 5) {
			 				$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
			 			} else {
			 				$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
			 			}
			 		}
	 				$(".page").append($str);
				}
	 			$(".page").append(' <a href="javascript:goToPage(\''+companyname+'\','+nextPage+')">下一页</a> ');
 				$(".page").append('<a href="javascript:goToPage(\''+companyname+'\','+totalPage+')">末页</a>');
		 	}
		}
	});
});

function goToPage(companyname,pagenum){
	$(".join_content").find("ul").empty();
	$(".page").empty();
	//加载单位
	var name = companyname;
	var num = pagenum;
	//alert(name);
	//alert(num);
	$.ajax({
		url : contextPath+ 'user/company/list',
		data : {
			companyName : companyname,
			pageszie : 4,
			pagenum : num	
		},
		type : 'POST',
		success : function(list) {
			var join = "";
			//alert(list.records.length);
			for(var i=0;i<list.records.length;i++){
				var company = list.records[i];
				var refstatus = company.refStatus;
				var refname = '<p class="event" onclick="join(\''+companyname+'\','+num+','+company.companyId+');">加入</p>';
				if(refstatus==1){
					refname = "<p>已加入</p>";
				}else if(refstatus==2){
					refname = "<p>待审核</p>";
				}
				join = join + "<li><a href='#'><span>"+company.companyId+" | "+company.companyName+"</span><em>"+company.address+"</em></a>"+refname+"</li>";
			}
			$(".join_content").find("ul").append(join);
			//分页
			var fullListSize = list.totalRecord;
		 	var pageNumber = list.currPage;
		 	var totalPage=0;
		 	if (fullListSize % 4 == 0) {
				totalPage = fullListSize / 4;
			} else {
				totalPage = Math.floor(fullListSize / 4 + 1);
			}
		 	//alert(fullListSize);
		 	//alert(pageNumber);
		 	//alert(totalPage);
		 	if(fullListSize!=undefined && fullListSize!=null && fullListSize>0){
		 		var start = 1;
	 			var end = totalPage;
	 			if(totalPage > 5 && pageNumber != 1) {
	 				if((totalPage-pageNumber)<3){
	 					start = pageNumber - (5 -(totalPage-pageNumber) -1);
	 				}else{
	 					start =  pageNumber -1;
	 				}
	 			} 
	 			if((totalPage-3)>pageNumber){
	 				if(pageNumber == 1) {
	 					end = 5;
	 				} else {
		 				end = pageNumber + 3;
	 				}
	 			}
	 			var prePage = pageNumber - 1 < 1 ? 1 : pageNumber - 1;
	 			var nextPage = pageNumber + 1 > totalPage ? totalPage : pageNumber + 1;
	 			
	 			$(".page").append('<a href="joincompany.jsp">首页</a> ');
	 			$(".page").append('<a href="javascript:goToPage(\''+companyname+'\','+prePage+')">上一页</a> ');
	 			for(var i=start;i<=end;i++){
		 			if(i == pageNumber){
		 				if(i < 5) {
		 					$str = $('<a class="intro" href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
		 				} else {
		 					$str = $('<a class="intro" href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
		 				}
			 		}else{
			 			if(i < 5) {
			 				$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
			 			} else {
			 				$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
			 			}
			 		}
	 				$(".page").append($str);
				}
	 			$(".page").append(' <a href="javascript:goToPage(\''+companyname+'\','+nextPage+')">下一页</a> ');
 				$(".page").append('<a href="javascript:goToPage(\''+companyname+'\','+totalPage+')">末页</a>');
		 	}
		}
	});
}

function join(companyname,pagenum,companyid){
	var name = companyname;
	var num = pagenum;
	var id = companyid;
	$.ajax({
		url : contextPath+ 'user/companyuser/addcompany',
		data : {
			companyid : id
		},
		type : 'POST',
		success : function(data) {
			alert(data.msgDesc);
			goToPage(name,num);
		}
	});
}

function querycomany(){
	var name = $("#searchname").val();
	//alert(name);
	if(name==""){
		alert("请输入查询条件");
		return false;
	}
	$(".join_content").find("ul").empty();
	$(".page").empty();
	//加载单位
	$.ajax({
		url : contextPath+ 'user/company/list',
		data : {
			companyName : name,
			pageszie : 4,
			pagenum : 1
		},
		type : 'POST',
		success : function(list) {
			var companyname = name;
			var join = "";
			for(var i=0;i<list.records.length;i++){
				var company = list.records[i];
				//alert(company.refStatus);
				var refstatus = company.refStatus;
				var refname = '<p class="event" onclick="join(\''+companyname+'\',1,'+company.companyId+');">加入</p>';
				if(refstatus==1){
					refname = "<p>已加入</p>";
				}else if(refstatus==2){
					refname = "<p>待审核</p>";
				}
				join = join + "<li><a href='#'><span>"+company.companyId+" | "+company.companyName+"</span><em>"+company.address+"</em></a>"+refname+"</li>";
			}
			$(".join_content").find("ul").append(join);
			//分页
			var fullListSize = list.totalRecord;
		 	var pageNumber = list.currPage;
		 	var totalPage=0;
		 	if (fullListSize % 4 == 0) {
				totalPage = fullListSize / 4;
			} else {
				totalPage = Math.floor(fullListSize / 4 + 1);
			}
		 	//alert(fullListSize);
		 	//alert(pageNumber);
		 	//alert(totalPage);
		 	if(fullListSize!=undefined && fullListSize!=null && fullListSize>0){
		 		var start = 1;
	 			var end = totalPage;
	 			if(totalPage > 5 && pageNumber != 1) {
	 				if((totalPage-pageNumber)<3){
	 					start = pageNumber - (5 -(totalPage-pageNumber) -1);
	 				}else{
	 					start =  pageNumber -1;
	 				}
	 			} 
	 			if((totalPage-3)>pageNumber){
	 				if(pageNumber == 1) {
	 					end = 5;
	 				} else {
		 				end = pageNumber + 3;
	 				}
	 			}
	 			var prePage = pageNumber - 1 < 1 ? 1 : pageNumber - 1;
	 			var nextPage = pageNumber + 1 > totalPage ? totalPage : pageNumber + 1;
	 			
	 			$(".page").append('<a href="joincompany.jsp">首页</a> ');
	 			$(".page").append('<a href="javascript:goToPage(\''+companyname+'\','+prePage+')">上一页</a> ');
	 			for(var i=start;i<=end;i++){
		 			if(i == pageNumber){
		 				if(i < 5) {
		 					$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
		 				} else {
		 					$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
		 				}
			 		}else{
			 			if(i < 5) {
			 				$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
			 			} else {
			 				$str = $('<a href="javascript:goToPage(\''+companyname+'\','+i+')">'+i+'</a> ');
			 			}
			 		}
	 				$(".page").append($str);
				}
	 			$(".page").append(' <a href="javascript:goToPage(\''+companyname+'\','+nextPage+')">下一页</a> ');
 				$(".page").append('<a href="javascript:goToPage(\''+companyname+'\','+totalPage+')">末页</a>');
		 	}
		}
	});
}